﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
using ProgNetComponents.Data.SqlCe;

namespace Wydatki.WRK.Praca.Raporty
{
    public partial class KpirSmall : DevExpress.XtraReports.UI.XtraReport
    {
        Dictionary<int, Dictionary<int, decimal>> dic = new Dictionary<int, Dictionary<int, decimal>>();
        
        public KpirSmall()
        {
            InitializeComponent();
        }

        int rok;
        int miesiac;

        public KpirSmall(int rok, int miesiac)
        {
            InitializeComponent();
            this.PaperKind = System.Drawing.Printing.PaperKind.A4;
            this.rok = rok;
            this.miesiac = miesiac;
            this.pRok.Value = rok;
            this.pMiesiac.Value = System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.MonthNames[miesiac - 1];
            GetData();
            foreach (XRTableCell cell in xrTable3.Rows[0].Cells)
            {
                if (cell.Name.StartsWith("s"))
                {
                    cell.Summary = new XRSummary(SummaryRunning.Page, SummaryFunc.Custom, "{0:n2}");
                    cell.SummaryGetResult += new SummaryGetResultHandler(cell_SummaryGetResult);
                }
            }
            foreach (XRTableCell cell in xrTable3.Rows[1].Cells)
            {
                if (cell.Name.StartsWith("p"))
                {
                    cell.Summary = new XRSummary(SummaryRunning.Page, SummaryFunc.Custom, "{0:n2}");
                    cell.SummaryGetResult += new SummaryGetResultHandler(cell2_SummaryGetResult);
                }
            }
            foreach (XRTableCell cell in xrTable3.Rows[2].Cells)
            {
                if (cell.Name.StartsWith("r"))
                {
                    cell.Summary = new XRSummary(SummaryRunning.Page, SummaryFunc.Custom, "{0:n2}");
                    cell.SummaryGetResult += new SummaryGetResultHandler(cell3_SummaryGetResult);
                }
            }
            foreach (XRTableCell cell in xrTable3.Rows[3].Cells)
            {
                if (cell.Name.StartsWith("y"))
                {
                    cell.Summary = new XRSummary(SummaryRunning.Page, SummaryFunc.Custom, "{0:n2}");
                    cell.SummaryGetResult += new SummaryGetResultHandler(cell4_SummaryGetResult);
                }
            }
        }
        int sIdx = 0;
        int pIdx = 0;
        int rIdx = 0;
        int yIdx = 0;
        void cell_SummaryGetResult(object sender, SummaryGetResultEventArgs e)
        {
            Dictionary<int, decimal> h = null;
            XRTableCell cell = sender as XRTableCell;
            int idx = sIdx;
            e.Result = "";
            e.Handled = true;
            int s = int.Parse(cell.Name.Substring(cell.Name.Length - 1));
           // if (sIdx % 2 == 1)
            {
                if (cell != null)
                {
                    if (dic.ContainsKey(idx))
                    {
                        h = dic[idx];
                    }
                    else
                    {
                        h = new Dictionary<int, decimal>();
                        dic.Add(idx, h);
                    }
                    decimal dec = 0;
                    foreach (object o in e.CalculatedValues)
                    {
                        if (o.IsNotNull())
                            dec += o.AsDecimal();

                    }
                    h[s.AsInt()] = dec;
                    e.Result = dec;
                    e.Handled = true;
                }
            }
            
            if (s == 9)
                sIdx++;
        }
        void cell2_SummaryGetResult(object sender, SummaryGetResultEventArgs e)
        {
            e.Result = "";
            Dictionary<int, decimal> h = null;
            XRTableCell cell = sender as XRTableCell;
           
            decimal dec = 0;

            if (cell != null)
            {
                int s = int.Parse(cell.Name.Substring(cell.Name.Length - 1));
                for (int idx = 0; idx <= pIdx; idx++)
                {
                    if (dic.ContainsKey(idx - 1))
                    {
                        h = dic[idx - 1];
                    }
                    if (h != null && h.ContainsKey(s))
                    {
                        dec += h[s];
                    }
                }
                if (s == 1)
                {

                }

                if (pIdx > 1)
                {
                    e.Result = dec;
                }
                 if (s == 9)
                    pIdx++;
            }
            e.Handled = true;

        }
        void cell3_SummaryGetResult(object sender, SummaryGetResultEventArgs e)
        {
            Dictionary<int, decimal> h = null;
            XRTableCell cell = sender as XRTableCell;

            if (cell != null)
            {
                int s = int.Parse(cell.Name.Substring(cell.Name.Length - 1));
                decimal dec = 0;
                for (int idx = 0; idx <= rIdx; idx++)
                {
                    if (dic.ContainsKey(idx))
                        h = dic[idx];
                    if (h != null && h.ContainsKey(s))
                        dec += h[s];
                }
                if (s == 9)
                    rIdx++;
                e.Result = dec;
                e.Handled = true;
            }
        }
        void cell4_SummaryGetResult(object sender, SummaryGetResultEventArgs e)
        {
            Dictionary<int, decimal> h = null;
            XRTableCell cell = sender as XRTableCell;
            int idx = yIdx;
            e.Result = "";
            e.Handled = true;
            int s = int.Parse(cell.Name.Substring(cell.Name.Length - 1));
            if (cell != null)
            {
                decimal dec = 0;

                if (dic.ContainsKey(idx))
                {
                    h = dic[idx];
                    if (h.ContainsKey(s))
                        dec = h[s];
                }
                if (s != 3 && s != 9)
                {
                    dec += dsRepKpir1.SUMA_ROK.Rows[0][cell.DataBindings[0].DataMember.Split('.')[1]].AsDecimal();
                }
                else
                {
                    //[S_STP_KPIR_WSTIU] + [S_STP_KPIR_PP]
                    if (s == 3)
                        dec += dsRepKpir1.SUMA_ROK.Rows[0]["S_STP_KPIR_WSTIU"].AsDecimal() +
                            dsRepKpir1.SUMA_ROK.Rows[0]["S_STP_KPIR_PP"].AsDecimal();
                    else
                    {
                        //[S_STP_KPIR_KRIR] + [S_STP_KPIR_WYN] + [S_STP_KPIR_PW]
                        dec += dsRepKpir1.SUMA_ROK.Rows[0]["S_STP_KPIR_KRIR"].AsDecimal() +
                            dsRepKpir1.SUMA_ROK.Rows[0]["S_STP_KPIR_WYN"].AsDecimal() +
                            dsRepKpir1.SUMA_ROK.Rows[0]["S_STP_KPIR_PW"].AsDecimal();
                    }
                }
                e.Result = dec;
                e.Handled = true;
            }

            if (s == 9)
                yIdx++;
        }
        private void GetData()
        {
            string sel = "SELECT STP_KPIR.*, STP_KONTRACHENCI.STP_KON_NAZWA, " +
                "STP_KONTRACHENCI.STP_KON_ADRES FROM STP_KPIR, STP_KONTRACHENCI WHERE " +
                " STP_KON_ID = STP_KPIR_KONTRACHENT AND YEAR(STP_KPIR_DATA) = " + rok + " AND MONTH(STP_KPIR_DATA) = " + miesiac;

            string sel2 = "SELECT SUM(case STP_KPIR_WSTIU when null then 0 else STP_KPIR_WSTIU end) AS S_STP_KPIR_WSTIU, " +
                "SUM (case STP_KPIR_PP when null then 0 else STP_KPIR_PP end ) AS S_STP_KPIR_PP, " +
                "SUM (case STP_KPIR_ZT when null then 0 else STP_KPIR_ZT end ) AS S_STP_KPIR_ZT, " +
                "SUM (case STP_KPIR_KOSZTY_UBOCZNE when null then 0 else STP_KPIR_KOSZTY_UBOCZNE end ) AS S_STP_KPIR_KOSZTY_UBOCZNE, " +
                "SUM (case STP_KPIR_KRIR when null then 0 else STP_KPIR_KRIR end ) AS S_STP_KPIR_KRIR, " +
                "SUM (case STP_KPIR_WYN when null then 0 else STP_KPIR_WYN end ) AS S_STP_KPIR_WYN, " +
                "SUM (case STP_KPIR_PW when null then 0 else STP_KPIR_PW end ) AS S_STP_KPIR_PW " +
                " FROM STP_KPIR WHERE YEAR(STP_KPIR_DATA) = " + rok + " AND MONTH(STP_KPIR_DATA) < " + miesiac;
            using (SqlCeDataManager updater = new SqlCeDataManager())
            {
                updater.FillTable(dsRepKpir1.STP_KPIR, sel);
                updater.FillTable(dsRepKpir1.SUMA_ROK, sel2);
            }
        }

    }
}
